package com.loongxun.playwithyou.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Order;
import com.loongxun.playwithyou.bean.OrderMeg;
import com.loongxun.playwithyou.ui.activity.user.UserOrderInfoActivity;
import com.loongxun.playwithyou.util.ToastView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/11.
 */
public class UserOrderAdapter extends BaseAdapter {

    private Context context;
    private List<Order> list;
    private LayoutInflater inflater;
    private int type = 0;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public Handler parentHandler;

    public UserOrderAdapter(Context context, List<Order> list, int type) {
        this.context = context;
        this.list = list;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<Order> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_user_order, null);
            holder.ll_item_order = (LinearLayout) convertView.findViewById(R.id.ll_item_order);
            holder.item_user_orderno = (TextView) convertView.findViewById(R.id.item_user_orderno);
            holder.item_user_orderdate = (TextView) convertView.findViewById(R.id.item_user_orderdate);
            holder.item_user_cycle = (TextView) convertView.findViewById(R.id.item_user_cycle);

            holder.tv_item_order_ok = (TextView) convertView.findViewById(R.id.tv_item_order_ok);
            holder.tv_item_order_cancel = (TextView) convertView.findViewById(R.id.tv_item_order_cancel);
            holder.tv_item_order_examine = (TextView) convertView.findViewById(R.id.tv_item_order_examine);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Order item = list.get(position);

        if (type == 0){
            holder.tv_item_order_ok.setVisibility(View.VISIBLE);
            holder.tv_item_order_ok.setText("确认支付");
            holder.tv_item_order_cancel.setVisibility(View.VISIBLE);
            holder.tv_item_order_cancel.setText("取消订单");
            holder.tv_item_order_examine.setVisibility(View.GONE);
        } else if (type == 1){
            holder.tv_item_order_ok.setVisibility(View.GONE);
            holder.tv_item_order_cancel.setVisibility(View.GONE);
            holder.tv_item_order_examine.setVisibility(View.GONE);
            try {
                Calendar calendarNew = Calendar.getInstance();
                calendarNew.setTime(new Date());
                Calendar calendarNew1 = Calendar.getInstance();
                calendarNew1.setTime(new Date());
                calendarNew1.add(Calendar.HOUR, 1);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(sdf.parse(item.getPlay_start_time()));
                //a比b早返回-1,a与b相同返回0,a比b晚返回1
                if (calendarNew1.compareTo(calendar) == -1){
                    if ("1".equals(item.getIsExamine())) {
                        item.setStart(5);
                        holder.tv_item_order_ok.setVisibility(View.GONE);
                    }else {
                        item.setStart(1);
                        holder.tv_item_order_ok.setText("申请取消");
                        holder.tv_item_order_ok.setVisibility(View.VISIBLE);
                    }
                }
                if (calendar.compareTo(calendarNew) == -1){
                    item.setStart(6);
                    holder.tv_item_order_ok.setText("陪练结束");
                    holder.tv_item_order_ok.setVisibility(View.VISIBLE);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (type == 2){
            holder.tv_item_order_ok.setVisibility(View.VISIBLE);
            holder.tv_item_order_ok.setText("评价");
            holder.tv_item_order_cancel.setVisibility(View.GONE);
            holder.tv_item_order_examine.setVisibility(View.GONE);
        } else if (type == 3){
            holder.tv_item_order_ok.setVisibility(View.VISIBLE);
            holder.tv_item_order_ok.setText("查看详情");
            holder.tv_item_order_cancel.setVisibility(View.GONE);
            holder.tv_item_order_examine.setVisibility(View.GONE);
        } else if (type == 4){
            holder.tv_item_order_examine.setVisibility(View.VISIBLE);
            holder.tv_item_order_ok.setVisibility(View.GONE);
            holder.tv_item_order_cancel.setVisibility(View.GONE);
            if (item.getExamine_state() == 0) {
                item.setStart(2);
                holder.tv_item_order_examine.setText("审核中");
            }else if (item.getExamine_state() == 1) {
                item.setStart(3);
                holder.tv_item_order_examine.setText("已通过");
            }else if (item.getExamine_state() == 2) {
                item.setStart(4);
                holder.tv_item_order_examine.setText("未通过");
            }
        }

        holder.item_user_orderno.setText("订  单  号："+item.getOrder_no());
        holder.item_user_orderdate.setText("预约时间："+item.getPlay_start_time());
        holder.item_user_cycle.setText("预约时长：共计" + item.getHours_num() + "小时");

        holder.tv_item_order_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message msg;OrderMeg orderMeg;
                switch (type){
                    case 0:
                        orderMeg = new OrderMeg();
                        orderMeg.setOrder_no(item.getOrder_no());
                        msg = new Message();
                        msg.what = 10;
                        msg.obj = orderMeg;
                        parentHandler.handleMessage(msg);
                        break;
                    case 1:
                        if (item.getStart() == 1) {
                            orderMeg = new OrderMeg();
                            orderMeg.setOrder_no(item.getOrder_no());
                            orderMeg.setStart(item.getStart());
                            orderMeg.setOrderState(type);
                            msg = new Message();
                            msg.what = 11;
                            msg.obj = orderMeg;
                            parentHandler.handleMessage(msg);
                        }else if (item.getStart() == 5){
                            new ToastView(context, "申请处理中").show();
                        }else {
                            orderMeg = new OrderMeg();
                            orderMeg.setOrder_no(item.getOrder_no());
                            orderMeg.setStart(item.getStart());
                            orderMeg.setOrderState(type);
                            msg = new Message();
                            msg.what = 3;
                            msg.obj = orderMeg;
                            parentHandler.handleMessage(msg);
                        }
                        break;
                    case 2:
                        orderMeg = new OrderMeg();
                        orderMeg.setOrder_no(item.getOrder_no());
                        orderMeg.setStart(item.getStart());
                        orderMeg.setOrderState(type);
                        msg = new Message();
                        msg.what = 11;
                        msg.obj = orderMeg;
                        parentHandler.handleMessage(msg);
                        break;
                    case 3:
                        orderMeg = new OrderMeg();
                        orderMeg.setOrder_no(item.getOrder_no());
                        orderMeg.setStart(item.getStart());
                        orderMeg.setOrderState(type);
                        msg = new Message();
                        msg.what = 11;
                        msg.obj = orderMeg;
                        parentHandler.handleMessage(msg);
                        break;
                    default:
                        break;
                }
            }
        });

        //取消订单
        holder.tv_item_order_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderMeg orderMeg = new OrderMeg();
                orderMeg.setOrder_no(item.getOrder_no());
                Message msg = new Message();
                msg.what = 0;
                msg.obj = orderMeg;
                parentHandler.handleMessage(msg);
            }
        });

        holder.ll_item_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderMeg orderMeg = new OrderMeg();
                orderMeg.setOrder_no(item.getOrder_no());
                orderMeg.setStart(item.getStart());
                orderMeg.setOrderState(type);
                Message msg = new Message();
                msg.what = 11;
                msg.obj = orderMeg;
                parentHandler.handleMessage(msg);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView item_user_orderno,item_user_orderdate,item_user_cycle
                ,tv_item_order_ok,tv_item_order_cancel,tv_item_order_examine;
        private LinearLayout ll_item_order;
    }

}
